package cn.kxm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.kxm.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import cn.kxm.service.CompanyService2;
import cn.kxm.service.OutstorageBillService2;
import cn.kxm.service.OutstorageBillService3;
import cn.kxm.service.PartService2;
import cn.kxm.service.impl.MakeNewCode2;

@Controller
@RequestMapping("/osb2")
public class OutstorageBillController2 extends BaseController{

	@Autowired
	private PartService2 ps;
	@Autowired
	private CompanyService2 cs;
	@Autowired
	private OutstorageBillService3 os;
	@Autowired
	private MakeNewCode2 mc;
	
	@RequestMapping("show")
	@ResponseBody
	public Object show(
			@RequestParam(value="findcode",required=false)String code,
			@RequestParam(value="findstartDate",required=false)String start,
			@RequestParam(value="findendDate",required=false)String end,
			@RequestParam(value="findsupplierName",required=false)String name,
			Integer page,Integer rows){
		int total=os.getCount(code,start,end,name);
		List<OutstorageBill> list=new ArrayList<OutstorageBill>();
		list=os.findAll(code,start,end,name,(page-1)*rows,rows);
		Map<String, Object> map=new HashMap<String, Object>();
		
		int amount=0;
		int money=0;
		for (OutstorageBill o : list) {
			amount+=o.getAmount();
			money+=o.getMoney();
		}
		OutstorageBill o=new OutstorageBill();
		o.setAmount(amount);
		o.setMoney(money);
		o.setCustomName("合计");
		o.setCode("true");
		OutstorageBill[] oo={o};
		map.put("footer", oo);
		map.put("rows", list);
		map.put("total", total);
		return JSONArray.toJSONString(map);
	}
	@RequestMapping("company")
	@ResponseBody
	public Object company(
			@RequestParam(value="findcode",required=false)String code,
			@RequestParam(value="findsupplierName",required=false)String name,
			@RequestParam(value="findtype",required=false)Integer type,
			Integer page,Integer rows){
		int total=cs.getCount(code,name,type);
		List<Company> list =cs.findAll(code,name,type,(page-1)*rows, rows);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return JSON.toJSONString(map);
	}
	
	@RequestMapping("/findallpart")
	@ResponseBody
	public Object findAllPart(@RequestParam(value="partBrand",required=false)String partBrand,
			@RequestParam(value="partNo",required=false)String partNo,
			@RequestParam(value="partName",required=false)String partName,
			@RequestParam(value="partTypeId",required=false)Integer partTypeId,
			Integer page,Integer rows){
		
		List<Part> list=ps.getPartList(partBrand, partNo, partName, partTypeId, (page-1)*rows, rows);
		int total=ps.getPartCount(partBrand, partNo, partName, partTypeId);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("total",total);
		map.put("rows",list);
		return JSONArray.toJSONString(map);
	}
	@RequestMapping("findpart")
    @ResponseBody
    public Object findAllTypeName(){
		
    	List<PartType> list=ps.getPartTypeList();
    	return JSON.toJSONString(list);
    }
	
	@RequestMapping("findone")
	public String findone(String code,Model model){
		OutstorageBill oBill=os.findByCode(code);
		model.addAttribute("oB", oBill);
		return "web/goods/showOuttorageBill";
	}
	@RequestMapping("modifyone")
	public String modifyone(String code,Model model){
		OutstorageBill oBill=os.findByCode(code);
		model.addAttribute("oB", oBill);
		return "web/goods/modifyOuttorageBill";
	}
	@RequestMapping("code")
	@ResponseBody
	public Object code(String prefix){
		Map<String, Object> map=new HashMap<String, Object>();
		String code=mc.getRandomCodeByPrefix(prefix);
		map.put("result", code);
		return JSONArray.toJSONString(map);
	}
	@RequestMapping("getpart")
	@ResponseBody
	public Object getpart(String code){
		System.out.println("wqwq");
		List<Map<String, Object>> list=os.getPart(code);
		System.out.println(JSONArray.toJSONString(list));
		return JSONArray.toJSONString(list);
	}
	@RequestMapping("add")
	@ResponseBody
	public Object add(OutstorageBill o,String data,String mm){
		Map<String, Object> map=new HashMap<String, Object>();
	
		if(os.addoB(o)!=1){
			map.put("result", "error");
			return JSON.toJSONString(map);
		}
		List<Map<String, Object>> list=(List<Map<String, Object>>) JSON.parse(data);
		List<String> memos=(List<String>)JSON.parse(mm);
		if(list!=null){
			for(int i=0;i<list.size()-1;i++){
				Map<String, Object> m=list.get(i);
				String ms=memos.get(i);
				OutstorageBillDetail obd=new OutstorageBillDetail();
				obd.setAmount((Integer)m.get("amount"));
				obd.setPartCode(m.get("code").toString());
				obd.setPrice((Integer)m.get("price"));
				obd.setBillCode(o.getCode());
				obd.setMemo(ms);
				if(os.addoBdetail(obd)!=1){
					map.put("result", "error");
					return JSON.toJSONString(map);
				}
			}
		}else{
			map.put("result", "error");
			return JSON.toJSONString(map);
		}
		map.put("result", "success");
		return JSON.toJSONString(map);
	}
	@RequestMapping("modify")
	@ResponseBody
	public Object modify(OutstorageBill o,String data,String mm){
		Map<String, Object> map=new HashMap<String, Object>();
		if(os.modifyoB(o)!=1){
			map.put("result", "error");
			return JSON.toJSONString(map);
		}
		List<Map<String, Object>> list=(List<Map<String, Object>>) JSON.parse(data);
		List<String> memos=(List<String>)JSON.parse(mm);
		if(list!=null){
			os.deleteoBdetail(o.getCode());//通过billcode删除明细
				//循环添加
				for (int i = 0; i < list.size()-1; i++) {
					Map<String,Object> m=list.get(i);
					String ms=memos.get(i);
					OutstorageBillDetail obd=new OutstorageBillDetail();
					obd.setAmount(Integer.parseInt(m.get("amount").toString()));
					obd.setPartCode(m.get("code").toString());
					obd.setPrice(Double.parseDouble(m.get("price").toString()));
					obd.setBillCode(o.getCode());
					obd.setMemo(ms);
					if(os.addoBdetail(obd)>0){
						map.put("result", "success");
					}else{
						map.put("result", "error");
						return JSON.toJSONString(map);
					}
				}
			
		}else{
			map.put("result", "error");
			return JSON.toJSONString(map);
		}
		
		return JSON.toJSONString(map);
	}
	@RequestMapping("delete")
	@ResponseBody
	public Object delete(String code){
		Map<String, Object> map=new HashMap<String, Object>();
		if(os.deleteoBdetail(code)>0&&os.deleteoB(code)>0){//先删除涉及外键的详单
			map.put("result", "success");
		}else{
			map.put("result", "error");
		}
		return JSON.toJSONString(map);
	}
	@RequestMapping("del")
	@ResponseBody
	public Object del(String data){
		Map<String, Object> map=new HashMap<String, Object>();
		List<Map<String, Object>> list=(List<Map<String, Object>>) JSON.parse(data);
		for (Map<String, Object> map2 : list) {
			if(os.deleteoBdetail(map2.get("code").toString())>0&&os.deleteoB(map2.get("code").toString())>0){//先删除涉及外键的详单
				map.put("result", "success");
			}else{
				map.put("result", "error");
			}
		}
		
		return JSON.toJSONString(map);
	}
	
	
	
	
	
	
	
}
